# import corresponding module
import pygame
from pygame.locals import *
import time,random


class HeroPlane:
	'''plane class'''
	
	def __init__(self,screen_temp):
		self.x = 200
		self.y = 400
		self.screen = screen_temp
		self.image = pygame.image.load("./images/me.png")
		self.bullet_list = [] # used to store the list of player's bullet
		self.boom_image = pygame.image.load("./images./boom.jpg") #随便找的图片，意思一下。

	def display(self):
		# draw the heroplane in the window		
		self.screen.blit(self.image,(self.x,self.y))
		# draw the herobullet in the window
		for b in self.bullet_list:
			b.display() # draw the bullet
			if b.move():
				self.bullet_list.remove(b)

	def move_left(self):
		'''move left and judge to prevent crossing'''
		self.x-=5
		if self.x<0:
			self.x=0

	def move_right(self):
		'''move right and judge to prevent crossing'''
		self.x+=5
		if self.x>406:
			self.x=406

	def fire(self):
		self.bullet_list.append(Bullet(self.screen,self.x,self.y))
		print("herobullet:"+str(len(self.bullet_list)))

	def move(self,enemylist):		
		# traverse the bullets and do collision detection
		for em in enemylist:
			for bo in em.bullet_list:
				if bo.x>self.x+12 and bo.x<self.x+92 and bo.y>self.y+20 and bo.y<self.y+60:
					em.bullet_list.remove(bo)
					return True

	def disappear(self):
		'''绘制我机爆炸效果'''
		self.screen.blit(self.boom_image,(self.x,self.y))


class Bullet:
	'''bullet class'''
	
	def __init__(self,screen_temp,x,y):
		self.x = x+53
		self.y = y
		self.screen = screen_temp
		self.image = pygame.image.load("./images/pd.png")
	
	def display(self):
		'''draw the plane in the window'''
		self.screen.blit(self.image,(self.x,self.y))

	def move(self):
		self.y-=10
		if self.y<20:
			return True


class EnemyPlane:
	'''enemy class'''
	
	def __init__(self,screen_temp):
		self.x = random.choice(range(408))
		self.y = -75
		self.screen = screen_temp
		self.image = pygame.image.load("./images./e"+str(random.choice(range(3)))+".png")
		self.bullet_list = []
		self.boom_image = pygame.image.load("./images./boom.jpg") #随便找的图片，意思一下。

	def display(self):
		# draw the enemyplane
		self.screen.blit(self.image,(self.x,self.y))
		# draw the enemybullet
		for b in self.bullet_list:
			b.display()
			if b.move():
				self.bullet_list.remove(b)

	def move(self,hero):
		self.y+=4
		# traverse the bullets and do collision detection
		for bo in hero.bullet_list:
			if bo.x>self.x+12 and bo.x<self.x+92 and bo.y>self.y+20 and bo.y<self.y+60:
				hero.bullet_list.remove(bo)
				return True
		# determine if the enemy plane has crossed the line
		if self.y>512:
			return True 

	def fire(self):
		self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y))
		print("enemybullet:"+str(len(self.bullet_list)))

	def disappear(self):
		'''绘制敌机爆炸效果'''
		self.screen.blit(self.boom_image,(self.x,self.y))


class EnemyBullet: # the size of plane 116*82
	''''''
	def __init__(self,screen_temp,x,y):
		self.x = x+58
		self.y = y+82
		self.screen = screen_temp
		self.image = pygame.image.load("./images/pd.png")

	def display(self):
		'''绘制子弹'''
		self.screen.blit(self.image,(self.x,self.y))
	
	def move(self):
		self.y += 5
		if self.y >= 588:
			return True


def key_control(hero_temp):
	'''keyboard control function'''
	
	# conducting quiting operation
	for event in pygame.event.get():
		if event.type == QUIT:
			print('exit')
			exit()

	# receive keyboard input
	pressed_keys = pygame.key.get_pressed()
	if pressed_keys[K_LEFT] or pressed_keys[K_a]:
		print('Left...')
		hero_temp.move_left()
	elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
		print('Right...')
		hero_temp.move_right()
	if pressed_keys[K_SPACE]:
		print('Space...')
		hero_temp.fire()

	# determine and conduct operation to object


def main():
	'''main function'''

	# create a game window
	screen = pygame.display.set_mode((512,568),0,0)

	# create game background
	background = pygame.image.load("./images/bg2.jpg")
	# initialize the Y value of the game background pictrue's coordinate axis
	m=-968 

	# create a player plane object
	hero = HeroPlane(screen)

	# create a list of enemyplane
	enemylist = []

	while True:

		# draw bmp
		screen.blit(background,(0,m))
		m+=2
		if m>=-200:
			m = -968

		# display hero plane
		hero.display()

		# conduct keyboard control function
		key_control(hero)

		# random output enemyplane
		if random.choice(range(50))==10:
			enemylist.append(EnemyPlane(screen))
		# check if the heroplane is shoot
		if hero.move(enemylist):
			hero.disappear()
			print('Game Over')
			exit()
		# traverse, show, move enemyplanes
		for em in enemylist:
			em.display()
			if random.choice(range(50))==5:# 随机发子弹，应该改为定时。
				em.fire()
			if em.move(hero):
				em.disappear()
				enemylist.remove(em)

		# update screen
		pygame.display.update()

		# timing sleeping
		time.sleep(0.04)



# determine if the current is the main running function and call it
if __name__ == "__main__":
	main() 